dfs and similar dsu graphs *1500

Please click on ads to support us..

Python Code:

def bfs(x):
    q = set()
    visited[x] = 1
    q.add(x)
    c = 0
    n = 1
    while len(q) != 0:
        v = q.pop()
        if not visited[v]:
            n += 1
        visited[v] = 1
        for i in g[v]:
            if not visited[i]:
                q.add(i)
                c += 1
    return c, n


n, m = map(int, input().split())
g = {}
visited = [0 for _ in range(n+1)]

for _ in range(m):
    a, b = map(int, input().split())
    try:
        g[a].append(b)
    except:
        g[a] = [b]

    try:
        g[b].append(a)
    except:
        g[b] = [a]

y = 'YES'
for i in g.keys():
        y, n = bfs(i)
        if y != (n*(n-1))//2:
        y = 'NO'
        break
else:
    y = "YES"

print(y)

C++ Code:

#include <iostream>
#include <vector>
#include <string>
#include <iomanip>
#include <math.h>
#include <map>
#include <set>
#include <limits>
#include <algorithm>
#include <cfenv>
#include <sstream>
#include <fstream>
#include <queue>

// #define ll long long

// #define yes "YES"
// #define no "NO"
#define en endl
#define phb push_back
using namespace std;
using ll = long long;
using u32 = unsigned int;
using u64 = unsigned long long;
// using i128 = __int128;

//--------------------------------------//

// https://trap.jp/post/1224/
#define FOR1(a) for (ll _ = 0; _ < ll(a); ++_)
#define FOR2(i, a) for (ll i = 0; i < ll(a); ++i)
#define FOR3(i, a, b) for (ll i = a; i < ll(b); ++i)
#define FOR4(i, a, b, c) for (ll i = a; i < ll(b); i += (c))
#define FOR1_R(a) for (ll i = (a)-1; i >= ll(0); --i)
#define FOR2_R(i, a) for (ll i = (a)-1; i >= ll(0); --i)
#define FOR3_R(i, a, b) for (ll i = (b)-1; i >= ll(a); --i)
#define overload4(a, b, c, d, e, ...) e
#define overload3(a, b, c, d, ...) d
#define FOR(...) overload4(__VA_ARGS__, FOR4, FOR3, FOR2, FOR1)(__VA_ARGS__)
#define FOR_R(...) overload3(__VA_ARGS__, FOR3_R, FOR2_R, FOR1_R)(__VA_ARGS__)

template <typename T, typename U>
vector<T> cumsum(vector<U> &A, int off = 1)
{
    int N = A.size();
    vector<T> B(N + 1);
    FOR(i, N) { B[i + 1] = B[i] + A[i]; }
    if (off == 0)
        B.erase(B.begin());
    return B;
}

template <class T>
constexpr T infty = 0;
template <>
constexpr int infty<int> = 1'000'000'000;
template <>
constexpr ll infty<ll> = ll(infty<int>) * infty<int> * 2;
template <>
constexpr u32 infty<u32> = infty<int>;
template <>
constexpr u64 infty<u64> = infty<ll>;
// template <>
// constexpr i128 infty<i128> = i128(infty<ll>) * infty<ll>;
template <>
constexpr double infty<double> = infty<ll>;
template <>
constexpr long double infty<long double> = infty<ll>;

template <typename T, typename U>
T ceil(T x, U y)
{
    return (x > 0 ? (x + y - 1) / y : x / y);
}

template <typename T, typename U>
T floor(T x, U y)
{
    return (x > 0 ? x / y : (x - y + 1) / y);
}

template <typename T, typename U>
T SUM(const vector<U> &A)
{
    T sum = 0;
    for (auto &&a : A)
        sum += a;
    return sum;
}

void YES(bool t = 1)
{
    string s;
    s = (t) ? "YES" : "NO";
    cout << s << en;
}
void NO(bool t = 1) { YES(!t); }
void Yes(bool t = 1)
{
    string s;
    s = (t) ? "Yes" : "No";
    cout << s << en;
}
void No(bool t = 1) { Yes(!t); }
void yes(bool t = 1)
{
    string s;
    s = (t) ? "yes" : "no";
    cout << s << en;
}
void no(bool t = 1) { yes(!t); }
unsigned int factorial(unsigned int n)
{
    unsigned int ret = 1;
    for (int i = 1; i <= n; ++i)
        ret *= i;
    return ret;
}
ll divisors(ll n)
{
    ll s = 0;
    for (int i = 1; i * i <= n; i++)
    {
        if (n % i == 0)
        {
            if (i != n / i)
            {
                s += 2;
            }
            else
                s++;
        }
    }
    return s;
}
//--------------------------------------//
ll n, m, x, y, nodes, edges;
vector<bool> visited(150001, false);

void dfs(int node, const vector<vector<int>> &g)
{
    visited[node] = 1;
    nodes++;
    for (auto i : g[node])
    {
        edges++;
        if (!visited[i])
            dfs(i, g);
    }
}

int main()
{

    cin >> n >> m;
    vector<vector<int>> g(n + 1, vector<int>());

    FOR1(m)
    {
        cin >> x >> y;
        g[x].push_back(y);
        g[y].push_back(x);
    }

    for (int i = 1; i <= n; i++)
    {
        nodes = 0;
        edges = 0;
        if (!visited[i])
        {
            dfs(i, g);

            edges /= 2;
            ll corr = (nodes * (nodes - 1)) / 2;
            if (edges != corr)
            {
                NO(1);
                return 0;
            }
        }
    }

    YES(1);

    return 0;
}


Comments

Submit
0 Comments
More Questions

1283B - Candies Division
1451B - Non-Substring Subsequence
1408B - Arrays Sum
1430A - Number of Apartments
1475A - Odd Divisor
1454B - Unique Bid Auction
978C - Letters
501B - Misha and Changing Handles
1496A - Split it
1666L - Labyrinth
1294B - Collecting Packages
1642B - Power Walking
1424M - Ancient Language
600C - Make Palindrome
1669D - Colorful Stamp
1669B - Triple
1669A - Division
1669H - Maximal AND
1669E - 2-Letter Strings
483A - Counterexample
3C - Tic-tac-toe
1669F - Eating Candies
1323B - Count Subrectangles
991C - Candies
1463A - Dungeon
1671D - Insert a Progression
1671A - String Building
1671B - Consecutive Points Segment
1671C - Dolce Vita
1669G - Fall Down